package io.sdl.controller;


import cn.hutool.core.date.DateTime;
import io.sdl.common.utils.ConvertUtils;
import io.sdl.common.utils.PageUtils;
import io.sdl.common.utils.Query;
import io.sdl.common.utils.Result;
import io.sdl.dto.SdlDiariesDTO;
import io.sdl.dto.SdlPatientsArchivesDTO;
import io.sdl.entity.SdlDiariesEntity;
import io.sdl.entity.SdlPatientsArchivesEntity;
import io.sdl.entity.SdlPatientsEntity;
import io.sdl.entity.TokenEntity;
import io.sdl.service.SdlPatientsArchivesService;
import io.sdl.service.TokenService;
import io.sdl.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


/**
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@RestController
@RequestMapping("/api")
public class ApiSdlPatientsArchivesController {
	@Autowired
	private SdlPatientsArchivesService sdlPatientsArchivesService;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private UserService userService;

	@Autowired
	private TokenService tokenServicel;

	/**
	 * 列表
	 */
	@GetMapping("/patientsArchives")
	@Operation(summary = "获取复查记录列表")
	public Result<PageUtils> patientsArchivesList(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
		if (params.get("token") != null && !params.get("token").equals("")) {

			TokenEntity byToken = tokenService.getByToken(params.get("token").toString());
			tokenService.expireToken(byToken.getUserId(),true);


			Long patientId = Long.valueOf(params.get("patientsId").toString());

			String filterDate = params.get("filterDate").toString();
			String sortOrder = params.get("sortOrder").toString();
			Query query = new Query(params);
			Integer total = sdlPatientsArchivesService.getNum(patientId);
			List<SdlPatientsArchivesDTO> patients = sdlPatientsArchivesService.getPatientsBypatientId(patientId, filterDate, query.getPage(), query.getLimit(), sortOrder);

			PageUtils pageUtils = new PageUtils(patients, total, query.getLimit(), query.getPage());

			return new Result<PageUtils>().ok(pageUtils);
		}

		return new Result<PageUtils>().error(400, "长时间未操作,请重新登录");
	}


	/**
	 * 信息
	 */
	@GetMapping("getRecheckById/{id}")
	@Operation(summary = "根据id获取复查信息")
	public Result getRecheckById(@PathVariable("id") Long id) {

		SdlPatientsArchivesEntity sdlPatientsArchivesEntity = sdlPatientsArchivesService.selectByArchivesId(id);

		return new Result().ok(sdlPatientsArchivesEntity);
	}

	/**
	 * 保存
	 */
	@PostMapping("recheck/save")
	@Operation(summary = "保存复查信息")
	public Result save(@RequestBody SdlPatientsArchivesDTO dto) {
		SdlPatientsArchivesEntity sdlPatientsArchivesEntity = ConvertUtils.sourceToTarget(dto, SdlPatientsArchivesEntity.class);
		sdlPatientsArchivesService.insert(sdlPatientsArchivesEntity);

		return new Result().ok(sdlPatientsArchivesEntity.getId());
	}

	@PostMapping("recheck/update")
	@Operation(summary = "编辑复查信息")
	private Result recheckUpdate(@RequestBody SdlPatientsArchivesDTO sdlPatientsArchivesDTO) {

		if (sdlPatientsArchivesDTO.getToken() != null && !sdlPatientsArchivesDTO.getToken().equals("")) {

			TokenEntity byToken = tokenService.getByToken(sdlPatientsArchivesDTO.getToken());
			SdlPatientsArchivesEntity sdlPatientsArchivesEntity = ConvertUtils.sourceToTarget(sdlPatientsArchivesDTO, SdlPatientsArchivesEntity.class);
			tokenService.expireToken(byToken.getUserId(),true);

			if (sdlPatientsArchivesDTO.getBraceDuration() != null && sdlPatientsArchivesDTO.getBraceDuration() != "") {
				sdlPatientsArchivesEntity.setBraceDurationCustom(null);
			}


//            sdlPatientsArchivesEntity.setXphoto((sdlPatientsArchivesDTO.getXphoto() != null && !sdlPatientsArchivesDTO.getXphoto().equals("")) ? sdlPatientsArchivesDTO.getXphoto() : null);
//            sdlPatientsArchivesEntity.setTbphoto((sdlPatientsArchivesDTO.getTbphoto() != null && !sdlPatientsArchivesDTO.getTbphoto().equals("")) ? sdlPatientsArchivesDTO.getTbphoto() : null);
			sdlPatientsArchivesService.updateById(sdlPatientsArchivesEntity);
			return new Result<>().ok(sdlPatientsArchivesEntity.getId());
		}
		return new Result().error(400, "长时间未操作,请重新登录");

	}


	@PostMapping("upload/recheck")
	@Operation(summary = "动态图片上传")
	public Result uploadrecheck(@RequestParam(value = "file", required = false) MultipartFile file, Long id, String picName) {
		try {
			// 1. 保存文件并返回地址
			sdlPatientsArchivesService.saveUploadedFile(file, id, picName);

			SdlPatientsArchivesEntity patientsArchivesEntity = sdlPatientsArchivesService.selectById(id);
			return new Result().ok(patientsArchivesEntity);
		} catch (Exception e) {
			return new Result().error(500, "文件上传失败: " + e.getMessage());

		}
	}

//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
//    @RequiresPermissions("generator:sdlpatientsarchives:update")
//    public R update(@RequestBody SdlPatientsArchivesEntity sdlPatientsArchives){
//        sdlPatientsArchivesService.updateById(sdlPatientsArchives);
//
//        return R.ok();
//    }
//

	/**
	 * 删除
	 */
	@GetMapping("/removeArchives/{id}")
	@Operation(summary = "删除患者复查记录")
	public Result removeArchives(@PathVariable("id") Long id) {
		sdlPatientsArchivesService.removeById(id);
		return new Result();

	}


}
